package com.ischoolbar.programmer.controller.admin;

import com.github.pagehelper.util.StringUtil;
import com.ischoolbar.programmer.entity.admin.Menu;
import com.ischoolbar.programmer.page.admin.Page;
import com.ischoolbar.programmer.service.admin.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    IMenuService menuService;

    /*
    * 返回列表页面
    * */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String menu_list(Model model) {
        /*
         * 顶级菜单
         * */
        model.addAttribute("topList", menuService.findTopList());
        return "menu/list";
    }

    /*
    * 菜单列表数据展示
    * */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> data_list(Page page,
                                         @RequestParam(value = "menu_name", required = false, defaultValue = "") String menu_name,
                                         Model model) {


        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();

        /*
        * 封装查询参数
        * */
        queryMap.put("menu_name", menu_name);
        queryMap.put("pageSize", page.getRows());
        queryMap.put("offset", page.getOffset());

        List<Menu> list = menuService.findList(queryMap);



        /*
        * 显示的数据
        * */
        ret.put("rows", list);
        /*
        * 显示数据总数
        * */
        ret.put("total", menuService.totalCount());
        return ret;
    }


    /*
    * 添加菜单处理
    * */
    @RequestMapping(value = "/add_menu", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Menu menu) {
        Map<String, String> ret = new HashMap<>();
        if (menu == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的菜单信息");
            return ret;
        }

        if (StringUtil.isEmpty(menu.getMenu_name())) {
            ret.put("type", "error");
            ret.put("msg", "菜单名称不能为空");
            return ret;
        }

        if (StringUtil.isEmpty(menu.getUrl())) {
            ret.put("type", "error");
            ret.put("msg", "菜单url不能为空");
            return ret;
        }

        if (menu.getParentId() == null) {
            menu.setParentId(0);
        }

        menuService.addMenu(menu);

        ret.put("type", "success");
        ret.put("msg", "菜单添加成功");
        return ret;
    }

    /*
    * 选择菜单icon
    * */
    @RequestMapping(value="/select_icon", method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> select_icon(HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<>();
        /*
        * 读取静态资源的icon图片返回给客户端
        * */
        String realPath = request.getServletContext().getRealPath("\\");
        File file = new File(realPath + "\\resources" + "\\admin" + "\\easyui" + "\\css" + "\\icons");
        if (!file.exists()) {
            ret.put("type", "error");
            ret.put("msg", "资源目录不存在");
            return ret;
        }

        File[] files = file.listFiles();
        List<String> iconsArr = new ArrayList<>();
        for (File f : files) {
            String filename = "icon-" +f.getName().substring(0, f.getName().indexOf(".")).replace("_", "-");
            iconsArr.add(filename);
        }

        ret.put("type", "success");
        ret.put("icons", iconsArr);
        return ret;
    }

    /*
    * 修改菜单信息
    * */
    @ResponseBody
    @RequestMapping(value="/edit_menu",method=RequestMethod.POST)
    public Map<String, String> edit(Menu menu) {
        Map<String, String> ret = new HashMap<>();
        if (menu == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的菜单信息");
            return ret;
        }

        if (StringUtil.isEmpty(menu.getMenu_name())) {
            ret.put("type", "error");
            ret.put("msg", "菜单名称不能为空");
            return ret;
        }

        if (menu.getParentId() == null) {
            menu.setParentId(0);
        }

        menuService.edit_menu(menu);

        ret.put("type", "success");
        ret.put("msg", "菜单修改成功");
        return ret;
    }

    /*
    * 删除菜单
    * */
    @RequestMapping("/delete_menu")
    @ResponseBody
    public Map<String, String> delete_menu(@RequestParam(value = "id", required = true) Integer id) {
        Map<String, String> ret = new HashMap<>();
        if (id == null) {
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的菜单信息");
            return ret;
        }

        List<Menu> childrenList = menuService.findChildrenList(id);
        if (childrenList != null && childrenList.size() > 0) {
            ret.put("type", "error");
            ret.put("msg", "所选定的菜单下包含子菜单不可以删除");
            return ret;
        }

        menuService.delete_menu(id);
        ret.put("type", "success");
        ret.put("msg", "删除成功");
        return ret;
    }
}
